package com.jiang.jixiang.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gyf.barlibrary.ImmersionBar;
import com.jiang.jixiang.R;
import com.jiang.jixiang.databinding.ActivityWelcomeBinding;
import com.ws.universal.tools.Constants;
import com.ws.universal.tools.utils.CacheUtils;
import com.ws.universal.tools.wsui.widget.QMUIRoundButton;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ActivityWelcomeBinding binding;
    private ViewPagerAdapter adapter;
    private Context context;
    private List views;
    private QMUIRoundButton startBtn;
    private int[] guidePics = {R.layout.view_guide1, R.layout.view_guide2, R.layout.view_guide3};//引导页中的三张图片布局
    private View[] dots;//代表视图索引的圆圈
    private int currentIndex;//表示前一个视图的索引

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //沉浸式
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                //.statusBarColor(R.color.app_color_blue)
                //.fitsSystemWindows(true)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init();
        binding = ActivityWelcomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        context = this;
        inItView();

        CacheUtils.setBoolean(Constants.isFristInstall, true);

        binding.tvSkip.setOnClickListener(v -> {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        // 如果切换到后台，就设置下次不进入功能引导页
        // SharePerferenceUtil.setFirstLogin(context, false);
    }


    private void inItView() {
        views = new ArrayList();
        initGuidePage();
        setViewPagerAdapter();
        initDots();

    }

    //初始化引导页视图
    private void initGuidePage() {
        for (int i = 0; i < guidePics.length; i++) {
            View view = LayoutInflater.from(context).inflate(guidePics[i], null);
            if (i == guidePics.length - 1) {
                startBtn = view.findViewById(R.id.GetStarted);
                startBtn.setChangeAlphaWhenPress(true);
                startBtn.setTag("start");
                startBtn.setOnClickListener(this);
            }
            views.add(view);
        }
    }

    //设置ViewPager适配器
    private void setViewPagerAdapter() {
        adapter = new ViewPagerAdapter(views);
        binding.viewPage.setAdapter(adapter);
        binding.viewPage.setOnPageChangeListener(this);
    }

    //初始化圆圈
    private void initDots() {
        dots = new View[guidePics.length];
        for (int i = 0; i < guidePics.length; i++) {
            dots[i] = binding.pointLayout.getChildAt(i);
            dots[currentIndex].setBackground(getDrawable(R.drawable.bg_welcome_focus));
            dots[i].setOnClickListener(this);
            dots[i].setTag(i);
        }
        currentIndex = 0;
        dots[currentIndex].setBackground(getDrawable(R.drawable.bg_welcome_focused));

    }


    @Override
    public void onClick(View v) {
        if (v.getTag().equals("start")) {

            startActivity(new Intent(this, MainActivity.class));
            finish();
            return;
        }
        //圆圈点击后执行
        int position = (Integer) v.getTag();
        setCurrentView(position);
        setCurrentDot(position);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 2) {
            binding.pointLayout.setVisibility(View.GONE);
        } else {
            binding.pointLayout.setVisibility(View.VISIBLE);
        }
        setCurrentDot(position);//设置圆圈状态
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //设置当前视图
    private void setCurrentView(int position) {
        if (position < 0 || position > guidePics.length - 1) {
            return;
        }
        binding.viewPage.setCurrentItem(position);
    }


    //设置当前圆圈状态
    private void setCurrentDot(int position) {
        if (position < 0 || position > guidePics.length - 1 || currentIndex == position) {
            return;
        }
        dots[currentIndex].setBackground(getDrawable(R.drawable.bg_welcome_focus));
        dots[position].setBackground(getDrawable(R.drawable.bg_welcome_focused));
        currentIndex = position;
    }


    public class ViewPagerAdapter extends PagerAdapter {
        private List<View> views;

        public ViewPagerAdapter(List<View> views2) {
            this.views = views2;
        }

        //获得视图总数
        @Override
        public int getCount() {
            if (views != null) {
                return views.size();
            }
            return 0;
        }

        //判断是否由对象生成视图
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return (arg0 == arg1);
        }

        //初始化position位置的视图
        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(views.get(position), 0);
            return views.get(position);
        }

        //销毁position位置的视图
        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(views.get(position));
        }
    }


}
